package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.domain.SelfTestRelationQuestion;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.question.mapper.SelfTestRelationQuestionMapper;
import com.wuwei.elearning.question.service.SelfTestService;
import com.wuwei.elearning.question.vo.TestVo;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学员自我练习时，用于保存自测记录试卷的表 前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-04-09
 */
@RestController
@RequestMapping("/elearning/selfTest")
@CrossOrigin
public class SelfTestController {

    @Resource
    private SelfTestService selfTestService;

    @Resource
    private SelfTestRelationQuestionMapper selfTestRelationQuestionMapper;

    @Resource
    private QuestionMapper questionMapper;




    /**
     * 自我测试生成考题的方法
     * @return 返回questionItemId
     * @Param selectedId 如果小于10000，则为chapter表中的id字段，如果大于10000，则为subject表的id加上10000
     */
    @GetMapping("batchInsert/{selectedId}/{testNum}")
    public Result<Long> batchInsertSelfTest(@PathVariable("selectedId") Long selectedId, HttpServletRequest request, @PathVariable("testNum") Integer testNum) {
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        Long id = selfTestService.batchInsert(selectedId,account,testNum);
        return Result.ok(id);
    }

    /**
     * 获取自我测试生成的考题的方法
     * @return 返回questionItemId
     * @Param  selfTestId self_test表的主键
     */
    @GetMapping("batchGet/{selfTestId}")
    public Result<Map<String,Object>> batchGet(@PathVariable("selfTestId") Long selfTestId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        Map<String,Object> result = selfTestService.batchGet(selfTestId,account);
        return Result.ok(result);
    }

    /**
     * 考完提交试卷的方法
     * @param testVo
     * @return
     */
    @PostMapping("submit")
    public Result<String> submit (@RequestBody TestVo testVo,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        Result<String> result = selfTestService.submit(testVo,account);
        return result;
    }

    /**
     * 根据self_test的主键先去self_test_relation_question查询和该主键关联的所有记录
     * 再去question表根据查询到的question_id查询记录
     * @param selfTestId self_test的主键
     * @return
     */
    @GetMapping("geBySelfTestId/{selfTestId}")
    public Result<List<Question>> geBySelfTestId(@PathVariable("selfTestId") Long selfTestId){
        LambdaQueryWrapper<SelfTestRelationQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SelfTestRelationQuestion::getSelfTestId,selfTestId);
        List<SelfTestRelationQuestion> selectList = selfTestRelationQuestionMapper.selectList(queryWrapper);

        List<Long> ids = new ArrayList<>();
        selectList.forEach(i->ids.add(i.getQuestionId()));

        List<Question> questions = questionMapper.selectBatchIds(ids);
        Map<Long, Question> questionMap = questions.stream().collect(Collectors.toMap(Question::getId, q -> q));
        List<Question> sortedQuestions = ids.stream().map(questionMap::get).collect(Collectors.toList());

        return Result.ok(sortedQuestions);
    }

    @DeleteMapping("deleteById/{selfTestId}")
    public Result<String> deleteById (@PathVariable("selfTestId") Long selfTestId,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        selfTestService.deleteById(selfTestId,account);
        return Result.ok("删除成功！");
    }

}

